package com.douqu.game.main.msg;

import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.entity.ext.BagInfo;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.protobuf.SGBagProto;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.protobuf.SGMainProto;
import com.google.protobuf.InvalidProtocolBufferException;
import org.springframework.stereotype.Component;
import com.douqu.game.main.util.MsgUtils;
import org.apache.log4j.Logger;

/**
 * Created by bean on 2017/7/21.
 */
@Component
public class BagMsgChannel implements AMsgChannel {

    Logger logger = Logger.getLogger(this.getClass());

    @Override
    public void messageChannel(int code, PlayerController playerController, byte[] data)
    {
        if(playerController == null)
            return;

        //背包
        BagInfo bagInfo = (BagInfo) playerController.getPlayer().getExtInfo(BagInfo.class);

        logger.info("code:" + code);
        //兵种升级
        if(code  ==  SGMainProto.E_MSG_ID.MsgID_Bag_CardLevelUp_VALUE){
            try {
                SGBagProto.C2S_CardLevelUp c = SGBagProto.C2S_CardLevelUp.parseFrom(data);
                bagInfo.cardLV(playerController, c == null ? 0 : c.getSid());
            } catch (InvalidProtocolBufferException e) {
                e.printStackTrace();
                logger.error(e);
            }
        }
        //我的卡包
        if(code  == SGMainProto.E_MSG_ID.MsgID_Bag_MyCard_VALUE){
            bagInfo.myCardList(playerController);

        }
        //卡组列表
        if(code  == SGMainProto.E_MSG_ID.MsgID_Bag_MyCardTeam_VALUE){
            bagInfo.myCardTeam(playerController);
        }
        //设置出阵列表
        if(code  == SGMainProto.E_MSG_ID.MsgID_Bag_SetBattleCardTeam_VALUE){
            try {
                SGBagProto.C2S_SetBattleCardTeam c = SGBagProto.C2S_SetBattleCardTeam.parseFrom(data);
                int cardTeamId = c == null ? ConstantFactory.ZERO : c.getCardTeamId();
                logger.info("cardTeamId:" + cardTeamId);
                bagInfo.setBattleCardTeam(playerController, cardTeamId);
            } catch (InvalidProtocolBufferException e) {
                e.printStackTrace();
            }
        }
        //替换卡片
        if(code  == SGMainProto.E_MSG_ID.MsgID_Bag_SetCardReplace_VALUE){

            try {
                SGBagProto.C2S_CardReplace c = SGBagProto.C2S_CardReplace.parseFrom(data);
                int cardTeamId = c == null ? ConstantFactory.ZERO : c.getCardTeamId();
                int activeCardId = c == null ? ConstantFactory.ZERO : c.getActiveCardId();
                int passiveCardId = c == null ? ConstantFactory.ZERO : c.getPassiveCardId();
                bagInfo.cardReplace(playerController,cardTeamId,activeCardId,passiveCardId);

            } catch (InvalidProtocolBufferException e) {
                e.printStackTrace();
                logger.error(e);
            }

        }

    }

}
